/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.core.fadingmessage.calibration.visual;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Toolkit;

import org.mopore.jat.gui.FadingMode;


public class InfoPainter {
	
	
	private static final String DRAG_TO_CALIBRATE_MESSAGE = "Drag the red window to calibrate its position.";

	private FadingMode currentFadingMode;

    private AlphaComposite standardComposite;
    
    private Font font;

    private final Color textColor;

    private static final float STANDARD_ALPHA = .85f;
    
    private int fadingIndex = 0;
    
    private static final int MAX_FADING = 3;
    
    private AlphaComposite[] fadingComposites = new AlphaComposite[4];

	private int centerX;

	private int centerY;
	
	private Integer messageX;
	
	private static final String HIT_ESCAPE_MESSAGE = "Hit the 'Esc' key when you have finished.";

	private int textWidth;
	
    public InfoPainter( ) {
    	
    	font = new Font( "Dialog", Font.BOLD, 18 );
        textColor = new Color( 150,0,0 );
        standardComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, STANDARD_ALPHA);
        
        float factor = 0.18f;
        for (int i = 0; i < fadingComposites.length; i++) {
        	float value = (i + 1) * factor;
        	fadingComposites[i] = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, value);
		}
        currentFadingMode = FadingMode.DONE;
        
        Toolkit t = Toolkit.getDefaultToolkit();
        double screenWidth = t.getScreenSize().getWidth();
        double screenHeight = t.getScreenSize().getHeight();
		centerX = (int)((screenWidth)/2);
		centerY = (int)((screenHeight)/2);
    }

    public void paintInfo(final Graphics2D g, final Composite origComposite, 
    		final CalibrationState calibtrationState) {

    	boolean show = checkCurrentState( calibtrationState );
    	
	    if ( show ){
	    	if ( messageX == null ){
	    		FontMetrics metrics = g.getFontMetrics( font );
	    		textWidth = metrics.stringWidth( HIT_ESCAPE_MESSAGE );
				messageX = centerX - (textWidth / 2);
	    	}
	
	        Composite composite = determineComposite();
			g.setComposite( composite );
	        
	        int boardWidth = textWidth + 140;
	        int boardHeight = 80;
	        int boardX = centerX - ( boardWidth / 2 );
	        int boardY = centerY - ( boardHeight / 2 ) - 20;
	
	        g.setColor( Color.WHITE );
	        g.fillRoundRect( boardX, boardY, boardWidth, boardHeight, 10, 10 );
	        g.setColor( Color.GRAY );
	        g.drawRoundRect( boardX, boardY, boardWidth, boardHeight, 10, 10 );
	        
	        g.setColor(textColor);
	        g.setFont( font );
	        
		    g.drawString( DRAG_TO_CALIBRATE_MESSAGE, messageX, centerY -30 );
	        g.drawString( HIT_ESCAPE_MESSAGE, messageX, centerY + 4 );
	        

	        g.setComposite(origComposite);
    	}
    }

	private boolean checkCurrentState( CalibrationState calibtrationState ) {
    	
		boolean isCalibrated = calibtrationState.isCalibrated();
		if ( isCalibrated ){
			return false;
		}
		
		boolean mouseIsOver = calibtrationState.isMoveOver();
		
		switch ( currentFadingMode ) {
			case FADING_IN: {
				if ( mouseIsOver ){
					fadingIndex++;
					if ( fadingIndex > MAX_FADING ){
						fadingIndex = MAX_FADING;
						currentFadingMode = FadingMode.PRESENT;
					}
				}
				else { // mouse is out
					currentFadingMode = FadingMode.DONE;
				}
				break;
			}
			case PRESENT: {
				if ( ! mouseIsOver ){
					currentFadingMode = FadingMode.FADING_OUT;
				}
				break;
			}
			case FADING_OUT: {
				if ( mouseIsOver ){
					currentFadingMode = FadingMode.FADING_IN;
				}
				else { // mouse is out
					fadingIndex --;
					if ( fadingIndex < 0 ){
						fadingIndex = 0;
						currentFadingMode = FadingMode.DONE;
					}					
				}
				break;
			}
			case DONE: {
				if ( mouseIsOver ){
					currentFadingMode= FadingMode.FADING_IN;
				}
				break;
			}
		}
		
		boolean show = currentFadingMode != FadingMode.DONE;
		return show;
	}

	private Composite determineComposite() {
		switch ( currentFadingMode ) {
			case FADING_IN: {
				return fadingComposites[fadingIndex];
			}
			case PRESENT: {
				return standardComposite;
			}
			case FADING_OUT: {
				return fadingComposites[fadingIndex];
			}
		}
		return null;
	}

}
